from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils.crypto import get_random_string


class User(AbstractUser):
    role_choices = (
        (1, '普通用户'),
        (2, '实验室管理员'),
        (3, '系统管理员'),
    )
    gender_choices = (
        ('1', '男'),
        ('2', '女'),
    )
    first_name = None
    last_name = None
    date_joined = None


    real_name = models.CharField('姓名', max_length=20)
    phone = models.CharField('手机号码', max_length=11, unique=True)
    email = models.EmailField('邮箱', unique=True)
    gender = models.CharField('性别', max_length=1, choices=gender_choices, default=1)
    role = models.IntegerField('角色', choices=role_choices, default=1)

    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    last_login = models.DateTimeField(null=True, blank=True)

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.real_name


class PasswordResetRequest(models.Model):
    user = models.ForeignKey(User,on_delete=models.SET_NULL,null= True, blank=True)     #关联用户id
    token = models.CharField(max_length=100, unique=True)
    created_at = models.DateTimeField(auto_now_add=True)    #请求时间
    is_processed = models.BooleanField(default=False)   #是否被同意

    class Meta:
        ordering = ['-created_at']

    def save(self, *args, **kwargs):
        if not self.token:
            self.token = get_random_string(50)
        super().save(*args, **kwargs)

    def __str__(self):
        return f"Reset request for {self.user.username}"
